<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板-达拉崩吧</title>
    <link rel="stylesheet" th:href="@{/css/semantic.min.css}">
    <link rel="stylesheet" th:href="@{/css/me.css}">
    <link rel="stylesheet" th:href="@{/css/readmaster.css}"/>
    <link rel="stylesheet" th:href="@{/font-awesome/css/font-awesome.min.css}">

</head>
<body>

<header th:replace="common::commonheader"></header>


<!--中间内容-->
<div id="waypoint" class="m-container-small m-padded-tb-big animated fadeIn">
    <div class="ui container">
        <div class="ui top attached segment">
           <div class="ui center aligned header">
               <h1>留言板</h1>
                <p>老板，来都来了，留下点儿什么吧</p>
           </div>
        </div>

        <div class="ui bottom attached segment">
            <div id="message-form" class="ui form">
                <input type="hidden" name="parentMessageId" value="-1">
                <div class="field">
                    <textarea name="content" placeholder="请输入留言信息..."></textarea>
                </div>
                <div>
                    <div class="fields">
                        <div class="field m-mobile-wide m-margin-bottom-small">
                            <div class="ui left icon input">
                                <a class="avatar m-container-small">
                                    <img name="avatar1" th:src="@{/image/qq.jpeg}"
                                         class="ui avatar image">
                                </a>
                            </div>
                        </div>
                        <div class="field m-mobile-wide m-margin-bottom-small">
                            <div class="ui left icon input">
                                <i class="qq icon"></i>
                                <input type="text" name="qq" placeholder="输入QQ号获取头像、昵称">
                            </div>
                        </div>
                        <div class="field  m-margin-bottom-small m-mobile-wide">
                            <button id="qqInfo" type="button" class="ui teal button m-mobile-wide"><i
                                    class="info icon"></i>获取
                            </button>
                        </div>
                        <div class="field  m-margin-bottom-small m-mobile-wide">
                            <button id="messagepost-btn" type="button" class="ui teal button m-mobile-wide"><i
                                    class="edit icon"></i>提交留言
                            </button>
                        </div>
                    </div>
                </div>
                <div id="qqInfo-form" th:fragment="qqInfo">
                    <input type="hidden"  name="avatar" th:value="${session.user}!=null ? ${session.user.avatar} : ${qqInfo?.avatar}">
                    <div class="fields">
                        <div class="field m-mobile-wide m-margin-bottom-small">
                            <div class="ui left icon input">
                                <a class="avatar m-container-small">
                                    <img th:src="${qqInfo!=null} ? ${qqInfo?.avatar} : @{/image/avatar.jpeg}"
                                         class="ui avatar image">
                                </a>
                            </div>
                        </div>
                        <div class="field m-mobile-wide m-margin-bottom-small">
                            <div class="ui left icon input">
                                <i class="user icon"></i>
                                <input type="text" name="nickname" placeholder="昵称"
                                       th:value="${session.user}!=null ? ${session.user.nickname} : ${qqInfo?.nickname}">
                            </div>
                        </div>
                        <div class="field m-mobile-wide m-margin-bottom-small">
                            <div class="ui left icon input">
                                <i class="mail icon"></i>
                                <input type="text" name="email" placeholder="邮箱"
                                       th:value="${session.user}!=null ? ${session.user.email} : ${qqInfo?.email}">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!--留言区域列表-->
            <div id="message-container" class="ui teal segment" th:fragment="messageList">

                <div>
                    <div class="ui threaded comments" style="max-width: 100%;">
                        <h3 class="ui dividing header">最新评论</h3>

                        <div class="comment" th:each="message : ${messages}">
                            <a class="avatar">
                                <img src="" th:src="${message.avatar}">
                            </a>
                            <div class="content">
                                <a class="author">
                                    <span th:text="${message.nickname}">Matt</span>
                                    <div class="ui mini basic teal left pointing label m-padded-mini"
                                         th:if="${message.webmaster}">站长
                                    </div>
                                </a>
                                <div class="metadata">
                                    <span class="date"
                                          th:text="${#dates.format(message.createTime,'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                                </div>
                                <div class="text" th:text="${message.content}">
                                    How artistic!
                                </div>
                                <div class="actions">
                                    <a class="reply" data-messageid="1" data-messagenickname="Matt"
                                       th:attr="data-messageid=${message.id},data-messagenickname=${message.nickname}"
                                       onclick="reply(this)">回复</a>
                                    <a th:if="${session.user!=null}" th:href="@{/messages/delete/{messageId}(messageId=${message.id})}">删除</a>
                                </div>
                            </div>

                            <div class="comments" th:if="${#arrays.length(message.replyMessages)}>0">
                                <div class="comment" th:each="reply : ${message.replyMessages}">
                                    <a class="avatar">
                                        <img src="" th:src="${reply.avatar}">
                                    </a>
                                    <div class="content">
                                        <a class="author">
                                            <span th:text="${reply.nickname}">小红</span>
                                            <div class="ui mini basic teal left pointing label m-padded-mini"
                                                 th:if="${reply.webmaster}">博主
                                            </div>
                                            &nbsp;<span th:text="|@ ${reply.parentMessage.nickname}|" class="m-teal">@ 小白</span>
                                        </a>
                                        <div class="metadata">
                                            <span class="date"
                                                  th:text="${#dates.format(reply.createTime,'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                                        </div>
                                        <div class="text" th:text="${reply.content}">
                                            How artistic!
                                        </div>
                                        <div class="actions">
                                            <a class="reply" data-commentid="1" data-commentnickname="Matt"
                                               th:attr="data-messageid=${reply.id},data-messagenickname=${reply.nickname}"
                                               onclick="reply(this)">回复</a>
                                            <a th:if="${session.user!=null}" th:href="@{/messages/delete/{messageId}(messageId=${reply.id})}">删除</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="toolbar" class="m-padded m-fixed m-right-bottom" style="display: none">
    <div class="ui vertical icon buttons ">
        <div id="toTop-button" class="ui icon button"><i class="chevron up icon"></i></div>
    </div>
</div>


<br>
<br>
<footer th:replace="common::commonfooter"></footer>


<script th:src="@{/js/jquery-1.7.2.min.js}"></script>
<script th:src="@{/js/semantic.min.js}"></script>
<script th:src="@{/js/jquery.scrollTo.min.js}"></script>
<script th:src="@{/lib/waypoints/jquery.waypoints.min.js}"></script>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/js/yss/gloable.js}"></script>


<script th:inline="javascript">


    $('#toTop-button').click(function () {
        $(window).scrollTo(0, 500);
    });



    //评论表单验证
    $('.ui.form').form({
        fields: {
            title: {
                identifier: 'content',
                rules: [{
                    type: 'empty',
                    prompt: '请输入留言内容'
                }
                ]
            }
        }
    });


    $('#qqInfo').click(function () {
        postQQInfo();
    });


    function postQQInfo() {
        $("#qqInfo-form").load(/*[[@{/message/qqinfo}]]*/"", {
            "qq": $("[name='qq']").val()
        })
    }


    $('#messagepost-btn').click(function () {
        var boo = $('.ui.form').form('validate form');
        if (boo) {
            console.log('校验成功');
            postData();
        } else {
            console.log('校验失败');
        }
    });

    function postData() {
        $("#message-container").load(/*[[@{/messages}]]*/"", {
            "parentMessageId": $("[name='parentMessageId']").val(),
            "avatar": $("[name='avatar']").val(),
            "nickname": $("[name='nickname']").val(),
            "content": $("[name='content']").val()
        }, function (responseTxt, statusTxt, xhr) {
            // $(window).scrollTo($('#comment-container'),500);
            clearContent();
        });
    }

    function clearContent() {
        $("[name='content']").val('');
        $("[name='parentMessageId']").val(-1);
        $("[name='content']").attr("placeholder", "请输入留言信息...");
    }



    function reply(obj) {
        var messageId = $(obj).data('messageid');
        var messageNickname = $(obj).data('messagenickname');
        $("[name='content']").attr("placeholder", "@" + messageNickname).focus();
        $("[name='parentMessageId']").val(messageId);
        $(window).scrollTo($('#info'), 500);
    }


</script>
</body>
</html>